package com.yc.damai.util;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;

/**
 * 
 * 	封装发送邮件工具类
 * 
 *	@ys
 */
public class EmailUtils {
	/**
	 * 	发送邮件
	 * @param receive		收件人邮箱
	 * @param title			邮件主题
	 * @param name          收件人的昵称
	 * @param content 		邮件内容
	 */
	public static void sendMail(String receive,String name,String title,String content) {
		//1.设置服务器的配置信息
		//发件人的账号
		String account = "3109862743@qq.com";
		//授权码
		String myPass = "bnarnnbxsvpddgba";
		//对会话进行设置
		Properties props = new Properties();
		//设置邮件发送协议,一般采用smtp协议
		props.put("mail.transport.protocol","smtp");
		//设置邮件服务器
		props.put("mail.host","smtp.qq.com");
		//是否开启smtp的认证功能
		props.put("mail.smtp.auth","true");
		//是否开启ssl加密功能   (可选)
		props.put("mail.smtp.ssl.enable","true");
		
		try {
			//设置socket连接工厂(可选)
			MailSSLSocketFactory factory = new MailSSLSocketFactory();
			//设置服务器是否是一个可以被信任的机器(可选)
			factory.setTrustAllHosts(true);
			props.put("mail.smtp.ssl.scoketFactory",factory);
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		}
		
		//2.建立会话
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			//进行与126/163/qq等邮件服务器之间的认证
			protected PasswordAuthentication getPasswordAuthentication() {
				//发件人的邮箱账号，注意此处密码不是邮箱密码是授权码
				return new PasswordAuthentication(account, myPass);
			}
		});
		
		try {
			//3.获取一个传输端口
			Transport transport  = session.getTransport();
			
			//4.连接服务器
			transport.connect(account, myPass);
			
			//5.创建Message对象
			MimeMessage message = createMsg(session,account,name,receive,title,content);
			
			//6.利用传输端口发送邮件
			transport.sendMessage(message,message.getAllRecipients());
			
			//7.关闭传输端口
			transport.close();
			}catch (MessagingException e) {
				e.printStackTrace();
		}
		
	}

	/**
	 * 创造邮件信息
	 * @param session
	 * @param account
	 * @param name
	 * @param receive
	 * @param title
	 * @param content
	 * @return
	 */
	private static MimeMessage createMsg(Session session,String account,String name,String receive,String title,String content) {
		
		MimeMessage message = new MimeMessage(session);
		
		try {
			//设置发件人
			message.setFrom(new InternetAddress(account,title,"UTF-8"));
			
			//设置收件人
			message.setRecipient(Message.RecipientType.TO,new InternetAddress(receive,name,"UTF-8"));
			
			//设置邮件的主题
			message.setSubject(title,"UTF-8");
			
			//设置邮件的内容
			message.setContent("<h1>尊敬的"+name+"女士(先生):"+content+"</h1>","text/html;charset=UTF-8");
			
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (Exception e) {//UnsupportedEncoding
			e.printStackTrace();
		}
		return message;
	}
	public static void main(String[] args) {
		sendMail("306529917@qq.com", "廖老师", "c94","恨不得尼丝纺内服好呢ufheuheuifheu");
	}
}
